﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace PSLib.Parsing.Common
{
    public static class EnumerableExtension
    { 
        public static IEnumerable<T> Convert<T>(this IEnumerable enumerable) where T: class
        {
            return new GenericEnumerable<T>(enumerable);
        }

    }

    public class GenericEnumerator<T> : IEnumerator<T> 
    {
        public GenericEnumerator(IEnumerator aggEnumerator)
        {
            this.aggEnumerator = aggEnumerator;
        }

        public T Current
        {
            get { return (T)aggEnumerator.Current; }
        }

        public void Dispose()
        {
        }

        object IEnumerator.Current
        {
            get { return aggEnumerator.Current; }
        }

        public bool MoveNext()
        {
            return aggEnumerator.MoveNext();
        }

        public void Reset()
        {
            aggEnumerator.Reset();
        }

        private IEnumerator aggEnumerator;
    }

    public class GenericEnumerable<T> : IEnumerable<T>
    {
        public GenericEnumerable(IEnumerable aggEnumerable)
        {
            this.aggEnumerable = aggEnumerable;
        }

        public IEnumerator<T> GetEnumerator()
        {
            return new GenericEnumerator<T>(aggEnumerable.GetEnumerator());
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return aggEnumerable.GetEnumerator();
        }

        private IEnumerable aggEnumerable;
    }
}
